package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.domain.ChatRoomEntity;
import com.domain.ChatUserRelaEntity;
import com.mapper.ChatRoomMapper;
import com.mapper.ChatUserRelaMapper;
import com.service.ChatUserRelaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 聊天室房间人员表 服务实现类
 * </p>
 *
 * @author sly
 * @since 2023-12-30
 */
@Service
public class ChatUserRelaServiceImpl extends ServiceImpl<ChatUserRelaMapper, ChatUserRelaEntity> implements ChatUserRelaService {

    @Autowired
    private ChatRoomMapper chatRoomMapper;

    @Autowired
    private ChatUserRelaMapper chatUserRelaMapper;

    @Override
    public List<ChatUserRelaEntity> getChatUsers(Long chatId) {
        QueryWrapper<ChatUserRelaEntity> chatQueryWrapper = new QueryWrapper<>();
        chatQueryWrapper.lambda().eq(ChatUserRelaEntity::getChatId, chatId);
        List<ChatUserRelaEntity> chatUserRelaEntities = chatUserRelaMapper.selectList(chatQueryWrapper);
        return chatUserRelaEntities;
    }

    @Override
    public Long getChatUserCount(Long chatId) {
        QueryWrapper<ChatUserRelaEntity> chatQueryWrapper = new QueryWrapper<>();
        chatQueryWrapper.lambda().eq(ChatUserRelaEntity::getChatId, chatId);
        return chatUserRelaMapper.selectCount(chatQueryWrapper);
    }

    @Override
    public List<ChatRoomEntity> getUserChats(Long userId) {
        List<ChatRoomEntity> list = chatUserRelaMapper.getUserChats(userId);
        return list;
    }

    @Override
    public void addChatUser(ChatUserRelaEntity chatUserRelaEntity) {
        chatUserRelaEntity.setCreateTime(new Date());
        chatUserRelaEntity.setUserLastIn(new Date());
        chatUserRelaMapper.insert(chatUserRelaEntity);

        QueryWrapper<ChatRoomEntity> chatQueryWrapper = new QueryWrapper<>();
        chatQueryWrapper.lambda().eq(ChatRoomEntity::getId, chatUserRelaEntity.getChatId())
                .eq(ChatRoomEntity::getIsDel, 0);
        ChatRoomEntity chat = chatRoomMapper.selectOne(chatQueryWrapper);
        chat.setPeopleNum(chat.getPeopleNum() + 1);
        chatRoomMapper.updateById(chat);
    }

    @Override
    public void updateChatUser(ChatUserRelaEntity chatUserRelaEntity) {
        chatUserRelaMapper.updateById(chatUserRelaEntity);
    }

    @Override
    public void deleteChatUser(ChatUserRelaEntity chatUserRelaEntity) {
        chatUserRelaMapper.deleteById(chatUserRelaEntity);

        QueryWrapper<ChatRoomEntity> chatQueryWrapper = new QueryWrapper<>();
        chatQueryWrapper.lambda().eq(ChatRoomEntity::getId, chatUserRelaEntity.getChatId())
                .eq(ChatRoomEntity::getIsDel, 0);
        ChatRoomEntity chat = chatRoomMapper.selectOne(chatQueryWrapper);
        chat.setPeopleNum(chat.getPeopleNum() - 1);
        chatRoomMapper.updateById(chat);
    }
}
